/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package security.beans;

import Entity.Profile;
import Entity.TableModelProfile;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import org.primefaces.event.RowEditEvent;
import security.business.ProfileBusiness;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@ViewScoped
public class EditProfileBean {
    Map<String, String> profiles;
    private String name;
    Profile profile;
    private TableModelProfile tableModel;
    Connection conn;
    
    public EditProfileBean() {
    }

    public Map<String, String> getProfiles() {
        return profiles;
    }

    public void setProfiles(Map<String, String> profiles) {
        this.profiles = profiles;
    }

    public Profile getProfile() {
        return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public TableModelProfile getTableModel() {
        return tableModel;
    }

    public void setTableModel(TableModelProfile tableModel) {
        this.tableModel = tableModel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    
    @PostConstruct
    private void init() {
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");
        profiles = new TreeMap<>();
        Connection connection = conectionBean.currentConnection;
        conn = connection;
        try {
            ProfileBusiness pb = new ProfileBusiness( connection );
            List<String> listAux = pb.getProfileName( );
            
            for(String s:listAux ){
                System.out.println(" NAME: "+s);
                profiles.put(s, s);
            }
            
            if( name!=null ){
                List<Profile> aux = pb.getProfile( name );
                tableModel = new TableModelProfile( aux );
            }
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Please Try Again!",
                    ex.getMessage()));
        }
    }
    
    public void delete(){
        ProfileBusiness pb = new ProfileBusiness(conn);
        System.out.println("DELETE: "+name);
        if( name==null || name.equals("")){
            FacesContext.getCurrentInstance().addMessage(
                        null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "No hay perfil Seleccionado",
                        "Seeccione un perfil."));
            return ;
        }
            
        try {
            pb.delete(name);
            FacesContext.getCurrentInstance().addMessage(
                        null,
                        new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "Perfil Eliminado",
                        "Operacion Exitosa"));
            profiles.remove( name );
            name = "DEFAULT";
            this.handleCityChange();
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                        null,
                        new FacesMessage(FacesMessage.SEVERITY_WARN,
                        "Please Try Again!",
                        ex.getMessage()));
        }
    }
    
    public void handleCityChange() {  
        System.out.println("ACA");
        if(name !=null && !name.equals(""))  {
            try {
                ProfileBusiness pb = new ProfileBusiness( conn );
                List<Profile> aux = pb.getProfile( name );
                tableModel = new TableModelProfile( aux );
                //profiles.remove( name );
            } catch (SQLException ex) {
                FacesContext.getCurrentInstance().addMessage(
                        null,
                        new FacesMessage(FacesMessage.SEVERITY_WARN,
                        "Please Try Again!",
                        ex.getMessage()));
            }
    
        } else  { 
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Please Try Again!",
                    ""));
        }
    }
    
    
    public void onEdit(RowEditEvent event ) {  
        FacesMessage msg;
        System.out.print("EDIT!!!!! >>>");
        
        System.out.println(" EVENT : " + profile.getResourceName());
        System.out.println(" EVENT : " + ((Profile) event.getObject()).getLimit());
        
        Profile nProfile = ((Profile) event.getObject());
        
        ProfileBusiness pb = new ProfileBusiness(conn);
        try {
            pb.update(profile.getProfile(), profile.getResourceName(), nProfile.getLimit()); 
            
            msg = new FacesMessage("Perfil editado!", "El perfil fue editado exitasamente");  
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            FacesContext.getCurrentInstance().addMessage(null, msg); 
            
        } catch (SQLException ex) {
            
            msg = new FacesMessage("Error!", "El perfil no pudo ser editado.");  
            msg.setSeverity(FacesMessage.SEVERITY_FATAL);
            FacesContext.getCurrentInstance().addMessage(null, msg); 
            onCancel(event);
        }
        
    }  
      
    public void onCancel(RowEditEvent event) {  
        
    }  

    
}
